package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.app.domain.AppAssetTransfer;
import com.ruoyi.app.domain.AppUser;
import com.ruoyi.app.domain.bo.*;
import com.ruoyi.app.domain.vo.*;
import com.ruoyi.app.service.*;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.redis.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 首页
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@RestController
public class SysIndexController {

    /**
     * 系统基础配置
     */
    private final RuoYiConfig ruoyiConfig;
    private final IAppApplyService applyService;
    private final IAppUserService iAppUserService;
    private final IAppUserCodeService iAppUserCodeService;
    private final IAppAssetService appAssetService;
    private final IAppAssetTransferService iAppAssetTransferService;
    private final IAppPackageOrderService iAppPackageOrderService;

    /**
     * 访问首页，提示语
     */
    @SaIgnore
    @GetMapping("/")
    public String index() {
        return StringUtils.format("欢迎使用{}后台管理框架，当前版本：v{}，请通过前端地址访问。", ruoyiConfig.getName(), ruoyiConfig.getVersion());
    }

    /**
     * 首页的统计数据
     *
     * @return
     */
    @GetMapping("indexData")
    public R<Object> indexData() {
        //当前用户总数
        HashMap<String, Object> map = new HashMap<>(4);
        int sum =  iAppUserCodeService.queryAuthed();
        map.put("userSum", sum);
        //今日新增用户总数
        Integer num = getXinZeng();
        map.put("newSum", num);
        //提现订单
        AppApplyBo applyBo = new AppApplyBo();
        applyBo.setStatus(Constants.SUCCESS);
        List<AppApplyVo> appApplyVos = applyService.queryList(applyBo);
        map.put("gourpSum", appApplyVos.size());
        //转增订单数量数量
        List<AppAssetTransferVo> transferVos = iAppAssetTransferService.queryList(new AppAssetTransferBo());
        map.put("dynamicSum", transferVos.size());
        //所有订单
        List<AppAssetVo> appAssetVos = appAssetService.queryList(new AppAssetBo());
        map.put("orderSum", appAssetVos.stream().map(AppAssetVo::getBalance).reduce(BigDecimal.ZERO, BigDecimal::add));
        map.put("paySum", appAssetVos.stream().map(AppAssetVo::getIntegral).reduce(BigDecimal.ZERO, BigDecimal::add));
        map.put("downSum", appAssetVos.stream().map(AppAssetVo::getLiveness).reduce(BigDecimal.ZERO, BigDecimal::add));
        map.put("cancelSum", appAssetVos.stream().map(AppAssetVo::getContribution).reduce(BigDecimal.ZERO, BigDecimal::add));
        //今日做任务数
        map.put("renwuSum",iAppPackageOrderService.queryTodaySum());
        //左日做任务数
        map.put("renwuYesSum",iAppPackageOrderService.queryYesTodaySum());
        //今日不做任务数量
        map.put("renwuNoSum", iAppPackageOrderService.queryTodayNoSum());
        //昨日不做任务数量
        map.put("renwuNoYesSum", iAppPackageOrderService.queryTodayNoYesSum());
        return R.ok(map);
    }

    private Integer getXinZeng() {
        // 获取当前日期的 LocalDate 对象
        LocalDate today = LocalDate.now();

        // 今天开始时间（00:00:00）
        LocalDateTime startOfDay = today.atStartOfDay();

        // 今天结束时间（23:59:59.999）
        LocalDateTime endOfDay = today.atTime(LocalTime.MAX);

        System.out.println("今天开始时间: " + startOfDay);
        System.out.println("今天结束时间: " + endOfDay);

        //今日注册的
        List<AppUser> appUsers = iAppUserService.selToday(startOfDay, endOfDay);
        //今日已经实名的
        Set<Long> users = iAppUserCodeService.selAuthed();

        int sum = 0;
        for (AppUser appUser : appUsers) {
            if (users.contains(appUser.getId())) {
                sum++;
            }
        }


        return sum;
    }
}
